<template>
	<view class="container">
		<!-- 用户信息区域 -->
		<view class="user-info-section" @click="checkLogin">
			<view class="user-info" v-if="isLogged">
				<image class="avatar" :src="userInfo.avatarUrl"></image>
				<view class="user-detail">
					<text class="nickname">{{userInfo.nickName}}</text>
					<text class="user-id">ID: {{userInfo.userId || '000000'}}</text>
				</view>
			</view>
			<view class="user-info" v-else>
				<image class="avatar" src="/static/logo.png"></image>
				<view class="user-detail">
					<text class="login-text">点击登录</text>
				</view>
			</view>
			<view class="user-stats">
				<view class="stat-item">
					<text class="stat-num">{{userInfo.visitCount || 0}}</text>
					<text class="stat-label">关注</text>
				</view>
				<view class="stat-item">
					<text class="stat-num">{{userInfo.likeCount || 0}}</text>
					<text class="stat-label">访客</text>
				</view>
				<view class="stat-item">
					<text class="stat-num">{{userInfo.favoriteCount || 0}}</text>
					<text class="stat-label">点赞</text>
				</view>
			</view>
		</view>
		
		<!-- 功能区域 -->
		<view class="function-section">
			<view class="function-group">
				<view class="function-item" @click="navigateTo('/pages/collect/collect')">
					<image class="function-icon" src="/static/logo.png"></image>
					<text class="function-text">收藏</text>
				</view>
				<view class="function-item" @click="navigateTo('/pages/history/history')">
					<image class="function-icon" src="/static/logo.png"></image>
					<text class="function-text">历史记录</text>
				</view>
				<view class="function-item" @click="navigateTo('/pages/guide/publish')">
					<image class="function-icon" src="/static/logo.png"></image>
					<text class="function-text">发布旅游攻略</text>
				</view>
				<view class="function-item" @click="navigateTo('/pages/order/order')">
					<image class="function-icon" src="/static/logo.png"></image>
					<text class="function-text">订单中心</text>
				</view>
				<view class="function-item" @click="navigateTo('/pages/settings/settings')">
					<image class="function-icon" src="/static/logo.png"></image>
					<text class="function-text">我的设置</text>
				</view>
			</view>
			
			<!-- 景点列表 -->
			<view class="attraction-section">
				<view class="section-header">
					<view class="section-title">最近浏览</view>
					<text class="more-btn" @click="navigateTo('/pages/history/history')">更多 ></text>
				</view>
				<view class="attraction-list">
					<view class="attraction-item" v-for="(item, index) in attractions" :key="index" @click="navigateTo('/pages/detail/detail?id=' + item.id)">
						<image class="attraction-image" :src="item.imageUrl"></image>
						<text class="attraction-name">{{item.name}}</text>
						<text class="attraction-desc">{{item.description}}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isLogged: false,
				userInfo: {},
				attractions: [
					{
						id: 1,
						name: '顺德博物馆',
						imageUrl: '/static/图片.jpg',
						description: '顺德 推荐'
					},
					{
						id: 2,
						name: '佛山祖庙',
						imageUrl: '/static/图片.jpg',
						description: '禅城 五星级'
					},
					{
						id: 3,
						name: '南风古灶',
						imageUrl: '/static/图片.jpg',
						description: '石湾 必打卡'
					}
				]
			}
		},
		onShow() {
			// 检查登录状态
			this.checkLoginStatus();
		},
		methods: {
			checkLoginStatus() {
				const token = uni.getStorageSync('token');
				if (token) {
					this.isLogged = true;
					this.getUserInfo();
				} else {
					this.isLogged = false;
				}
			},
			getUserInfo() {
				// 从缓存获取用户信息
				const userInfo = uni.getStorageSync('userInfo');
				if (userInfo) {
					this.userInfo = JSON.parse(userInfo);
				} else {
					// 从服务器获取用户信息
					uni.request({
						url: 'http://localhost:8080/api/user/info',
						method: 'GET',
						header: {
							'Authorization': uni.getStorageSync('token')
						},
						success: (res) => {
							if (res.data.code === 200) {
								this.userInfo = res.data.data;
								uni.setStorageSync('userInfo', JSON.stringify(res.data.data));
							}
						}
					});
				}
			},
			checkLogin() {
				if (!this.isLogged) {
					uni.navigateTo({
						url: '/pages/login/login'
					});
				}
			},
			navigateTo(url) {
				// 检查是否已登录
				const token = uni.getStorageSync('token');
				if (!token) {
					uni.navigateTo({
						url: '/pages/login/login'
					});
					return;
				}
				uni.navigateTo({
					url: url
				});
			}
		}
	}
</script>

<style>
	.container {
		min-height: 100vh;
		background-color: #e6f7ff;
		padding-bottom: 40rpx;
	}
	
	.user-info-section {
		background-color: #ffffff;
		padding: 40rpx 30rpx;
		border-radius: 0 0 20rpx 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}
	
	.user-info {
		display: flex;
		align-items: center;
		margin-bottom: 30rpx;
	}
	
	.avatar {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
		margin-right: 20rpx;
	}
	
	.user-detail {
		flex: 1;
	}
	
	.nickname {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 10rpx;
	}
	
	.user-id {
		font-size: 24rpx;
		color: #999;
	}
	
	.login-text {
		font-size: 36rpx;
		color: #333;
	}
	
	.user-stats {
		display: flex;
		justify-content: space-around;
		padding: 20rpx 0;
		border-top: 1px solid #f5f5f5;
	}
	
	.stat-item {
		display: flex;
		flex-direction: column;
		align-items: center;
	}
	
	.stat-num {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 10rpx;
	}
	
	.stat-label {
		font-size: 24rpx;
		color: #999;
	}
	
	.function-section {
		padding: 20rpx;
	}
	
	.function-group {
		display: flex;
		flex-wrap: wrap;
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 30rpx 20rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}
	
	.function-item {
		width: 25%;
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 20rpx;
	}
	
	.function-icon {
		width: 60rpx;
		height: 60rpx;
		margin-bottom: 10rpx;
	}
	
	.function-text {
		font-size: 24rpx;
		color: #666;
	}
	
	.attraction-section {
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 30rpx 20rpx;
		box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
	}
	
	.section-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
	}
	
	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		position: relative;
		padding-left: 20rpx;
	}
	
	.section-title:before {
		content: '';
		position: absolute;
		left: 0;
		top: 6rpx;
		width: 6rpx;
		height: 32rpx;
		background-color: #1296db;
	}
	
	.more-btn {
		font-size: 26rpx;
		color: #666;
	}
	
	.attraction-list {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
	}
	
	.attraction-item {
		width: 31%;
		margin-bottom: 20rpx;
	}
	
	.attraction-image {
		width: 100%;
		height: 180rpx;
		border-radius: 10rpx;
		margin-bottom: 10rpx;
	}
	
	.attraction-name {
		font-size: 26rpx;
		color: #333;
		margin-bottom: 6rpx;
	}
	
	.attraction-desc {
		font-size: 22rpx;
		color: #999;
	}
</style> 